<?php

namespace frontend\modules\farmerhouse\controllers;

use common\components\Helper;
use common\models\business\BusinessFarmers;
use common\models\business\BusinessUser;
use common\models\farmerhouse\FarmerhouseHouse;
use common\models\shop\ShopHouseOrder;
use common\models\sys\SysSetting;
use frontend\controllers\BaseMobileController;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;


/**
 * Class FarmerHoseController
 * @package frontend\modules\business\controllers
 * 农家乐相关控制器
 */
class OrderController extends BaseMobileController
{


    /**
     * @param $id
     * @return string
     * 订单确认页
     */
    public function actionConfirm($id)
    {

        $house = (new Query())->select('id,name as title,price,roomnum')->from(FarmerhouseHouse::tableName())->where('id=:id',[':id'=>$id])->one();

        $startdate = strtotime(date('Y-m-d'));
        $enddate = strtotime(date('Y-m-d',strtotime("+1 day")));
        $house['roomnum'] = FarmerhouseHouse::getLeftnum($id,$startdate,$enddate);


        //积分兑换金额比率
        $radio = SysSetting::getValue('scoretomoney');
        $user = BusinessUser::find()->select('score')->where('id=:id',[':id'=>$this->user['userid']])->asArray()->one();
        $house['reduce'] = $reduce = ($user['score']*$radio);

        //取出最多可以使用的积分
        $maxscore = SysSetting::getValue('canusemax_score');
        if($reduce > $maxscore){
            $house['reduce'] = $reduce = $maxscore;
            $user['score'] = intval($reduce/$radio);
        }
        $house['credit'] = $user['score'];

        return $this->render('@template/mobile/house_confirm.html',[
            'house' => \GuzzleHttp\json_encode($house),
            'reduce' => $reduce,
            'score' => $user['score']
        ]);
    }


    /**
     * @return array
     * 下单
     */
    public function actionPrePay()
    {

        $postdata = \Yii::$app->request->post();

        $house = (new Query())->select('price')->from(FarmerhouseHouse::tableName())->where('id=:id',[':id'=>$postdata['houseid']])->one();

        $days = Helper::dateDifference($postdata['start_date'],$postdata['end_date']);
        $price = $house['price'] * $days * intval($postdata['number']);


        //积分省去的钱
        $scoreprice = 0;
        if(array_key_exists('usecredit',$postdata) && !empty($postdata['usecredit'])){
            $radio = SysSetting::getValue('scoretomoney');
            $user = BusinessUser::find()->select('score')->where('id=:id',[':id'=>$this->user['userid']])->asArray()->one();
            if($user['score'] <= $postdata['usecredit']){
                return $this->jsonMsg('对不起您的积分不足',1);
            }
            $scoreprice = intval($postdata['usecredit']*$radio);


        }


        $data = [
            'name' => $postdata['name'],
            'mobile' => $postdata['tel'],
            'userid' => $this->user['userid'],
            'houseid' => $postdata['houseid'],
            'price' => $price,
            'startdate' => strtotime($postdata['start_date']),
            'enddate' => strtotime($postdata['end_date']),
            'days' => $days,
            'cost' => floatval($price - $scoreprice),
            'scoreprice' => $scoreprice,
            'planin' => $postdata['time'],
            'remark' => $postdata['remark'],
            'roomnum' => $postdata['number'],
            'ordersn' => date('ymdHi').$this->user['userid'].rand(10,99),
            'createtime' => time(),
        ];

        $res = \Yii::$app->db->createCommand()->insert(ShopHouseOrder::tableName(),$data)->execute();
        $orderid = \Yii::$app->db->lastInsertID;

        if($res){
            return $this->jsonMsg('下单成功',0,['url'=>Url::to(['/pay/wechat/pay/house','oid'=>$orderid])]);
        }else{
            return $this->jsonMsg('下单失败',1);
        }
    }



    public function actionRoomnum()
    {
        $postdata = \Yii::$app->request->post();
        $startdate = strtotime($postdata['data'][0]);
        $enddate = strtotime($postdata['data'][1]);
        $id = \Yii::$app->request->post('id');
        //查询所选日期是否有房
        $num = FarmerhouseHouse::getLeftnum($id,$startdate,$enddate);


        return \GuzzleHttp\json_encode(['num'=>$num]);



    }



}